package com.nondo.rdp.finance.endpoint.api.admin.setting.home;

import com.nondo.rdp.core.common.BeanUtils;
import com.nondo.rdp.core.common.CommUtil;
import com.nondo.rdp.core.common.ValidateUtil;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.setting.home.SettingModule;
import com.nondo.rdp.finance.logic.SettingModule.SettingModuleLogic;
import com.nondo.rdp.finance.model.setting.home.AdminSettingModuleBean;
import com.nondo.rdp.finance.service.analysis.AnalysisService;
import com.nondo.rdp.finance.service.setting.home.SettingModuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("apiAdminSettingHomeModule")
@RequestMapping("/api/admin/setting/home/module")
public class ModuleEndpoint {
    @Autowired
    SettingModuleService settingModuleService;

    @Autowired
    SettingModuleLogic settingModuleLogic;

    @Autowired
    AnalysisService analysisService;

    @Autowired
    CommUtil commUtil;

    @RequestMapping("/list")
    public View list() {
        View view = new View(20000, "获取列表成功");

        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        List<SettingModule> list = this.settingModuleService.findAll(sort);

        List newList = new ArrayList();
        for (SettingModule item : list) {
            //排除掉被删除的模块
            if (item.getDelFlag().equals("1")) {
                continue;
            }

            AdminSettingModuleBean adminSettingModuleBean = new AdminSettingModuleBean();
            BeanUtils.copy(item, adminSettingModuleBean);

            //处理更多类型的数据
            adminSettingModuleBean.setExtra(settingModuleLogic.getAdminTypeContent(item.getType(), item.getValue()));

            //处理图片
            adminSettingModuleBean.setIconFull(CommUtil.getFileUrl(adminSettingModuleBean.getIcon()));
            adminSettingModuleBean.setIcon(item.getIcon());

            newList.add(adminSettingModuleBean);
        }

        Map map = new HashMap();
        map.put("list", newList);
        map.put("typeList", settingModuleLogic.getHandTypeList());

        view.setData(map);

        return view;
    }

    @RequestMapping("/update")
    public View update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody AdminSettingModuleBean adminSettingModuleBean, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            //幻灯片永远放第一
            if (adminSettingModuleBean.getModuleType().equals(SettingModule.TYPE_SLIDE) && adminSettingModuleBean.getSort() > 0) {
                adminSettingModuleBean.setSort(0);
                return View.ofError("幻灯片永远排第一，修改排序无效");
            } else if (!adminSettingModuleBean.getModuleType().equals(SettingModule.TYPE_SLIDE) && adminSettingModuleBean.getSort() < 1) {
                return View.ofError("为保证幻灯片永远排第一，其他模块不能将排序设置到最前面");
            }

            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(adminSettingModuleBean.getType(), adminSettingModuleBean.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }

            SettingModule settingModule = new SettingModule();
            BeanUtils.copy(adminSettingModuleBean, settingModule);

            this.settingModuleService.update(settingModule);
        }

        return view;
    }

    @RequestMapping("/add")
    public View add(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody AdminSettingModuleBean adminSettingModuleBean, BindingResult result) {
        View view = new View(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            if (adminSettingModuleBean.getSort() < 1) {
                return View.ofError("为保证幻灯片永远排第一，其他模块不能将排序设置到最前面");
            }

            adminSettingModuleBean.setModuleType(SettingModule.TYPE_CUSTOMER);
            adminSettingModuleBean.setStatus(1); //新添加的模块默认为关闭状态，以防止APP首页出问题
            adminSettingModuleBean.setStyle(1);//默认添加都选择为样式一

            //判断提交的更多类型是不是有效的
            boolean checkTypeValue = settingModuleLogic.checkTypeValue(adminSettingModuleBean.getType(), adminSettingModuleBean.getValue());
            if (!checkTypeValue) {
                return View.ofError("操作类型和值不匹配");
            }

            SettingModule settingModule = new SettingModule();
            BeanUtils.copy(adminSettingModuleBean, settingModule);

            this.settingModuleService.save(settingModule);
        }

        return view;
    }

    @RequestMapping("/delete/{id}")
    public View delete(@PathVariable("id") Long id) {
        //判断删除的模块是不是自定义模块
        SettingModule settingModule = this.settingModuleService.findOne(id);
        if (!settingModule.getModuleType().equals(SettingModule.TYPE_CUSTOMER)) {
            return View.ofError("不能删除非自定义模块");
        }
        this.settingModuleService.delete(id);

        return View.ofOk("删除成功");
    }
}